package org.anupta.realm.server.inter.packet.connect;

import org.anupta.realm.main.Main;
import org.anupta.realm.server.inter.client.InterClient;
import org.anupta.realm.server.inter.packet.InterPacket;
import org.anupta.realm.server.realm.client.RealmClient;
import org.anupta.realm.server.realm.packet.login.answer.HostList;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

public class Connect extends InterPacket {
	public Connect(InterClient client,IoBuffer buffer) {
		super(client);
		unpack(buffer);
		doPacket(buffer);
	}

	@Override
	public void unpack(IoBuffer buffer) {
		client.setGuid(buffer.get());
		client.setType(buffer.get());
		client.setState(buffer.get());
		client.setPop(buffer.get());
		client.setIp(readString(buffer));
		client.setPort(buffer.getInt());
		Main.out.println(new StringBuilder().append("GameID : ").append(client.getGuid()).append(" connected").toString());
	}

	@Override
	public void pack(IoBuffer buffer) {
	}

	@Override
	public void doPacket(IoBuffer buffer) {
		for(IoSession session : Main.realm.getAllSession().values()){
			if(session.getAttribute("client") instanceof RealmClient){
				RealmClient client = (RealmClient) session.getAttribute("client");
				new HostList(client);
				continue;
			}
		}
	}
}
